package ge.info.pages;

import ge.info.domain.City;
import ge.info.domain.Country;
import ge.info.domain.transport.Transport;
import ge.info.domain.transport.TransportType;
import ge.ktapestry.pages.BasePageExt;
import java.util.Date;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

import java.util.List;
import org.apache.tapestry.event.PageEndRenderListener;

public abstract class TransportTypePage extends BasePageExt implements PageBeginRenderListener, PageEndRenderListener
{

    @Persist
    public abstract Transport getTransport();

    public abstract void setTransport(Transport transport);

//    @Persist
    public abstract List<Transport> getTransports();

    public abstract void setTransports(List<Transport> transports);

//    @Persist
    public abstract Country getOutCountry();

    public abstract void setOutCountry(Country outCountry);

//    @Persist
    public abstract Country getInCountry();

    public abstract void setInCountry(Country inCountry);
    public static int SERVICE_ID = 13;

    public abstract Transport getLoopTransport();

    public abstract void setLoopTransport(Transport transport);

    public abstract Date getFakeDate();

    public abstract void setFakeDate(Date date);

    public void pageEndRender(PageEvent event)
    {
        if (!event.getRequestCycle().isRewinding())
        {

            setInCountry(null);
            setOutCountry(null);
           setTransports(null);
        }
    }

    public void pageBeginRender(PageEvent event)
    {
        if (!event.getRequestCycle().isRewinding())
        {
            if (getTransport() == null || getTransport().getTransportType() == null)
            {
                Transport transport = new Transport();
                TransportType tType = (TransportType) getComboes().getCombo("TransportTypes").getOption(0);
                City city = new City();
                city.setCityID(2208);
                transport.setTransportType(tType);
                transport.setOutCity(city);
                city = new City();
                city.setCityID(2208);
                transport.setInCity(city);
                setTransport(transport);
            }
            if (getInCountry() == null)
            {
                Country country = new Country();
                country.setCountryID(194);
                setOutCountry(country);
                country = new Country();
                country.setCountryID(194);
                setInCountry(country);
            }
        }
    }

    public void searchTransport(IRequestCycle cycle)
    {
        setTransports(getDbExecutor().searchTransport(getTransport(), getSession()));

    }
public void sendSMS(IRequestCycle cycle, Transport transport)
    {

        StringBuilder sb = new StringBuilder();

        sb.append(transport.getOutCity().getCityName()).append(" ");
        if (transport.getOutPlace() != null)
        {
            sb.append(transport.getOutPlace()).append(" ");
        }
       
        if (transport.getOutTime() != null)
        {
            sb.append(transport.getOutTime()).append(" ");
        }
         sb.append(transport.getInCity().getCityName()).append(" ");

         if (transport.getInPlace() != null)
        {
            sb.append(transport.getInPlace()).append(" ");
        }

        if (transport.getInTime() != null)
        {
            sb.append(transport.getInTime()).append(" ");
        }
        if (transport.getDay() != null)
        {
            sb.append(transport.getDay());
        }
         if (transport.getNote() != null)
        {
            sb.append(transport.getNote());
        }
        getDbExecutor().insertSms(getSession(), sb.toString(), SERVICE_ID);
    }
    public void searchTransportByCountry(IRequestCycle cycle)
    {
        Transport transport = getTransport();
        int inID = transport.getInCity().getCityID();
        int outID = transport.getOutCity().getCityID();
        Country c1 = getInCountry();
        Country c2 = getOutCountry();
        transport.getInCity().setCityID(getInCountry().getCountryID());
        transport.getOutCity().setCityID(getOutCountry().getCountryID());
        transport.setInCountry(c1);
        transport.setOutCountry(c2);
        setTransports(getDbExecutor().searchTransportByCountry(transport, getSession()));
        transport.getInCity().setCityID(inID);
        transport.getOutCity().setCityID(outID);

    }

    public void openTransportStopsPage(IRequestCycle cycle, Integer id)
    {

        List list = getDbExecutor().searchTransportStops(id);
        IPage page = cycle.getPage("TransportStopsPage");
        PropertyUtils.write(page, "transports", list);
        cycle.activate(page);
    }

    public void searchTripCriteria(IRequestCycle cycle)
    {
        if (getTransport().getTripCriteria() == null)
        {
            setTransports(null);
            return;
        }
        setTransports(getDbExecutor().searchTripCriteria(getTransport()));
        getTransport().setTripCriteria(null);
    }
}
